﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using RLZY.others;
using Infragistics.Win.UltraWinGrid;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
namespace RLZY
{
    public partial class FormNotKey_GDX_Modify : Form
    {
        public FormNotKey_GDX_Modify()
        {
            InitializeComponent();
            
        }
        //public FormBDX_Modify(string username,String 权限)
        //{
        //    InitializeComponent();
        //    this.userID= strTextBox1Text;
        //}
        private static FormNotKey_GDX_Modify childFromInstanc;
        static log4net.ILog LOG = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public String userID;
        public String 权限;


        public static FormNotKey_GDX_Modify ChildFromInstanc
        {
            get
            {
                if (childFromInstanc == null || childFromInstanc.IsDisposed)
                //判断childFromInstanc是否有值，不为null或者窗体释放（当你将子窗体关了的时候）为childFromInstanc赋值
                {
                    childFromInstanc = new FormNotKey_GDX_Modify();
                }
                return childFromInstanc;
                //返回的childFromInstanc（子窗体存在时返回的是null,关闭了（IsDisposed为true）或者第一次打开子窗体返回一个ChildForm实例）
            }
        }

        private void FormNotKey_GDX_Modify_Load(object sender, EventArgs e)
        {
            try
            {
                BLL.userpassword bllpwd = new BLL.userpassword();
                Model.userpassword model = bllpwd.GetModel(Convert.ToInt32(others.UserHelper.UserID));
                others.myGZ mygz = new myGZ();
                权限 = model.备注;
                //this.txtBM.Text = 权限;

                //DataSet ds = mygz.GetListPart(0, 权限);
                //this.BindData(ds);
                this.BindListInfo();
            }
            catch (Exception ex) { LOG.Error(ex); }
        }

        private void BindListInfo()
        {
            others.myGZ mygz = new myGZ();
            others.NotKey notkey = new NotKey();
            cmbSearchBZ.Items.Clear();
            

            try
            {
                DataSet dsgz = mygz.GetListBM(权限);
                cmbSearchBZ.DataSource = dsgz.Tables[0];
                cmbSearchBZ.DisplayMember = "车间班组";  //列名1 
                cmbSearchBZ.ValueMember = "车间班组";    //列名2 
                DataSet dsbm = notkey.GetListBM();
                
                
            }
            catch (Exception ex)
            {
                LOG.Error(ex);
            }
        }

        private void BindData(DataSet ds)
        {
            DataView dv = new DataView();
            dv.Table = ds.Tables[0];
            //dv.Sort = "ID desc";//反排序
            //bindingSource1.DataSource = dv;
            this.ultraGrid1.DataSource = dv;
        }
        //列选择器
        private void btnCustomColumnChooser_Click(object sender, EventArgs e)
        {
            try
            {
                this.ShowCustomColumnChooserDialog();
            }
            catch (Exception ex) { LOG.Error(ex); }
        }
        private RLZY.CustomColumnChooser customColumnChooserDialog = null;
        
        private void ShowCustomColumnChooserDialog()
        {
            if (null == this.customColumnChooserDialog || this.customColumnChooserDialog.IsDisposed)
            {
                this.customColumnChooserDialog = new RLZY.CustomColumnChooser();
                this.customColumnChooserDialog.Owner = this;
                this.customColumnChooserDialog.Grid = this.ultraGrid1;
            }

            this.customColumnChooserDialog.Show();
        }
        //修改列布局
        private void btnDisplayLayout_Click(object sender, EventArgs e)
        {
            ultraGrid1.DisplayLayout.Bands[0].PerformAutoResizeColumns(true, PerformAutoSizeType.VisibleRows);
        }
        //隐藏列
        private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            e.Layout.Bands[0].Columns[0].Hidden = true;
        }

        private void cmbBZ_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }
        int id;

        private void ultraGrid1_DoubleClickCell(object sender, DoubleClickCellEventArgs e)
        {
            try
            {
                int i = this.ultraGrid1.ActiveRow.Index;
                id = Convert.ToInt32(this.ultraGrid1.Rows[i].Cells[0].Value.ToString().Trim());
                dtpDate.Text = this.ultraGrid1.Rows[i].Cells["日期"].Value.ToString().Trim();
                this.txtBM.Text = this.ultraGrid1.Rows[i].Cells["部门单位"].Value.ToString().Trim();
                this.txtBZ.Text = this.ultraGrid1.Rows[i].Cells["车间班组"].Value.ToString().Trim();
                this.txtName.Text = this.ultraGrid1.Rows[i].Cells["姓名"].Value.ToString().Trim();
                this.txtKHZJ.Text = this.ultraGrid1.Rows[i].Cells["扣互助金"].Value.ToString().Trim();
                this.txtNameID.Text = this.ultraGrid1.Rows[i].Cells["身份证号"].Value.ToString().Trim();
                this.txtZH.Text = this.ultraGrid1.Rows[i].Cells["帐号"].Value.ToString().Trim();
                this.txtPlace.Text = this.ultraGrid1.Rows[i].Cells["籍贯_出生地"].Value.ToString().Trim();
                this.txtCT.Text = this.ultraGrid1.Rows[i].Cells["车贴"].Value.ToString().Trim();
                this.txtXJJT.Text = this.ultraGrid1.Rows[i].Cells["星级津贴"].Value.ToString().Trim();
                this.txtBMDWBZ.Text = this.ultraGrid1.Rows[i].Cells["部门单位备注"].Value.ToString().Trim();

            }
            catch(Exception ex)
            {
                LOG.Error(ex);

            }

        }
        public void Last_Date()
        {
            others.myGZ mygz = new myGZ();
            DataSet dataSet = mygz.GetListLastDate(权限);
           
            bool flag = dataSet.Tables[0].Rows.Count > 0;
            if (flag)
            {
                this.LastDate = dataSet.Tables[0].Rows[0]["日期"].ToString();
            }
            else
            {
                this.LastDate = "9998-12-31";
            }
            
        }
        
        public string LastDate { get; set; }

        
      
        public string Dat { get; set; }

        public bool GZ_One_Modify()
        {
            BLL.GZ bll = new BLL.GZ();
            Model.GZ model = bll.GetModel(id);
            model.车间班组=this.txtBZ.Text .Trim ();
            model.姓名=this.txtName.Text .Trim ();
            model.扣互助金 = Convert.ToInt32(this.txtKHZJ.Text.Trim());
            model.帐号 = txtZH.Text.Trim();
            model.籍贯_出生地 = txtPlace.Text.Trim();
            model.车贴 =Convert.ToInt32( txtCT.Text.Trim());
            model.星级津贴 = Convert.ToInt32(txtXJJT.Text.Trim());
            model.部门单位备注 = txtBMDWBZ.Text.Trim();
            model.身份证号 = txtNameID.Text.Trim();
            model.五项扣款 = model.扣款1 + model.扣款2 + model.扣款3 + model.扣款4 + model.扣款5;
            model.应发总额 = model.技能工资 + model.岗位工资 + model.绩效工资 + model.粮贴 + model.回贴 + model.洗理费 + model.水电气补贴 + model.工龄津贴 + model.职称津贴 + model.特岗津贴 + model.行车工津贴 + model.卫生纸费 + model.车贴 + model.星级津贴 + model.津贴1 + model.津贴2 + model.津贴3 + model.津贴4 + model.津贴5 + model.补发 + model.中晚班费 + model.加班工资 - model.扣病假工资 - model.扣事假工资 - model.其它扣款;
            model.实发金额 = model.应发总额 - model.扣会费 - model.扣丧葬费 - model.扣互助金 - model.扣治安费 - model.扣电视费 - model.扣养老金 - model.扣失业保险 - model.扣医疗保险 - model.扣公积金 - model.扣款1 - model.扣款2 - model.扣款3 - model.扣款4 - model.扣款5;
            model.应发 = model.技能工资 + model.岗位工资 + model.绩效工资 + model.粮贴 + model.回贴 + model.洗理费 + model.水电气补贴 + model.工龄津贴 + model.职称津贴 + model.特岗津贴 + model.卫生纸费 + model.补发 + model.中晚班费 + model.加班工资 - model.扣病假工资 - model.扣事假工资 - model.其它扣款;
            model.实发 = model.应发 - model.扣会费 - model.扣丧葬费 - model.扣互助金 - model.扣治安费 - model.扣电视费 - model.扣养老金 - model.扣失业保险 - model.扣医疗保险 - model.扣公积金 - model.扣款5;
            if (model.部门单位 == "锻造厂" | (model.部门单位 == "特钢机关" & model.车间班组 == "驻外"))
            {
                model.应发总额 = model.技能工资 + model.岗位工资 + model.绩效工资 + model.粮贴 + model.回贴 + model.洗理费 + model.水电气补贴 + model.工龄津贴 + model.职称津贴 + model.特岗津贴 + model.行车工津贴 + model.卫生纸费 + model.车贴 + model.星级津贴 + model.津贴1 + model.津贴2 + model.津贴3 + model.津贴4 + model.津贴5 + model.补发 + model.中晚班费 + model.加班工资 - model.扣病假工资 - model.扣事假工资 - model.其它扣款;
                model.其它扣款 = model.其它扣款 + model.实发金额;
                model.实发金额 = model.应发总额 - model.扣会费 - model.扣丧葬费 - model.扣互助金 - model.扣治安费 - model.扣电视费 - model.扣养老金 - model.扣失业保险 - model.扣医疗保险 - model.扣公积金 - model.扣款1 - model.扣款2 - model.扣款3 - model.扣款4 - model.扣款5;
                model.应发 = model.应发总额;
                model.实发 = model.实发金额;
            }
           return bll.Update(model);
        }
        public bool NameIDIS_Exist()
        {
            others.NotKey not = new NotKey();
            SqlDataReader re = not.GetListNameId(this.txtNameID.Text.Trim());
            if (re.Read())
            {
                return true;
            }
            else
            {
                return false;
            }

        }
        public bool ZHIS_Exist()
        {
            others.NotKey not = new NotKey();
            SqlDataReader re = not.GetListZH(this.txtZH.Text.Trim());
            if (re.Read())
            {
                return true;
            }
            else
            {
                return false;
            }

        }
        //按回车键跳转

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Enter && !(ActiveControl is Button))
            {//碰到Button时不跳转到下一个控件，而是执行Button的单击事件
                keyData = Keys.Tab;
            }
            return base.ProcessDialogKey(keyData);
        }

       
        
        public string VarDat { get; set; }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            BLL.userpassword bllpwd = new BLL.userpassword();
            Model.userpassword model = bllpwd.GetModel(Convert.ToInt32(others.UserHelper.UserID));
            others.NotKey notkey = new NotKey();
            权限 = model.备注;

            DataSet ds = notkey.searchKey(this.权限, this.cmbSearchBZ.Text.Trim(),this.txtSearchName.Text .Trim ());
            this.BindData(ds);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                string strErr = "";
                if (this.txtBZ.Text.Trim().Length == 0)
                {
                    strErr += "班组不能为空！\r\n";
                }
                if (this.txtName.Text.Trim().Length == 0)
                {
                    strErr += "姓名不能为空！\r\n";
                }
                if (this.txtNameID.Text.Trim().Length == 0)
                {
                    strErr += "身份证号不能为空！\r\n";
                }
                if (NameIDIS_Exist())
                {
                    strErr += "身份证号码重复！\r\n";
                }
                if (this.ZHIS_Exist())
                {
                    strErr += "账号重复！\r\n";
                }

                this.Last_Date();

                DateTime time1 = DateTime.Parse(this.VarDat);
                DateTime time2 = DateTime.Parse(this.LastDate);

                if (DateTime.Compare(time1, time2) < 0)
                {
                    strErr += "已做保护！\r\n";
                }
                if (strErr != "")
                {
                    MessageBox.Show(this, strErr);
                    return;
                }


                if (this.GZ_One_Modify())
                {
                    Msg.DisplayMsg("提示", "新增成功。\r\n本提示窗口2秒钟后自动消失", 1000);
                    BLL.userpassword bllpwd = new BLL.userpassword();
                    Model.userpassword model = bllpwd.GetModel(Convert.ToInt32(others.UserHelper.UserID));
                    others.NotKey notkey = new NotKey();
                    权限 = model.备注;

                    DataSet ds = notkey.searchKey(this.权限, this.cmbSearchBZ.Text.Trim(), this.txtSearchName.Text.Trim());
                    this.BindData(ds);
                }
            }
            catch (Exception ex) { LOG.Error(ex); }
        }

        
    }
}
